//
//  CoursewareController.swift
//  amicool
//
//  Created by 方君竹 on 2017/6/15.
//  Copyright © 2017年 fjz. All rights reserved.
//

import UIKit

class CoursewareController: UITableViewController {
    
    var courseList:[AnyObject]!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.backgroundColor = UIColor(red:248/255, green:250/255, blue:254/255,alpha: 1)
        
        //修改导航条背景色及标题色
        self.navigationController?.navigationBar.barTintColor = UIColor(colorLiteralRed: 20/255.0, green: 155/255.0, blue: 1.0, alpha: 1.0)
        self.navigationController?.navigationBar.titleTextAttributes =
            [NSForegroundColorAttributeName: UIColor.white]
        self.title = "课件"
        
        //注册一个单元格coursecell
        self.tableView.register(CourseCell.self, forCellReuseIdentifier: "courseCell")
        
        getCoursewareList()
        

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }
    
    func getCoursewareList(){
        let paremeters:NSDictionary = ["SessionID":AMNetworkMngTool.loginReturnModel!.sessionid!,
                                       "mod":"tware",
                                       "page":"1"]
        AMNetworkMngTool.shared.AMNetwork_GetList(paremeters){ (list) in
            DispatchQueue.main.async {
                if list != nil{
                    self.courseList = [AnyObject]()
                    
                    //封装过程
                    for course in list!{
                        let model =  CoursewareReturnJson.coursewareListModelWithDict(dict: course as? NSDictionary)
                        self.courseList.append(model!)
                    }
                    self.tableView.reloadData()//刷新
                }
                else{
                    //提示错误
                }
            }
            
        }
        
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    //单元格数量根据courselist决定
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if self.courseList != nil{
            return self.courseList.count
        }
        // #warning Incomplete implementation, return the number of rows
        return 0
    }
    
    //配置表的过程
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        //获取表的单元格，并转成自定义单元格的类别
        let cell  = tableView.dequeueReusableCell(withIdentifier: "courseCell", for: indexPath) as! CourseCell
        
        // Configure the cell...不给它点中的效果
        cell.selectionStyle = .none
        
        
        //如何配置单元格 对属性进行相应的赋值
        if self.courseList != nil{
            let model = self.courseList[indexPath.row] as! CoursewareReturnJson
            //cell.img.image = model 课件设计到的图片
            if model.thumb == nil || model.thumb!.hasPrefix(".pdf"){
                cell.img.image = UIImage(named: "images/Placeholder.png")
            }
            else {
                let imgUrlPre = "http://amicool.neusoft.edu.cn/UPloads/"
                let imgUrl = URL(string: (imgUrlPre + model.thumb!))
                let imgData = try! Data(contentsOf: imgUrl!)
                cell.img.image = UIImage(data: imgData)?.scaleImageToSize(CGSize(width: 80, height: 60))
                
            }
            
            //model从courselist 的课件里表数组中取
            cell.title.text = model.name //课件标题
            cell.autor.text = model.author //作者
            cell.updateTime.text = model.update_time //更新时间
            
            
        }
        
        return cell
    }
    
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 110 //单元格高度
    }
    
    //对uiviewcontroller的方法进行重写，状态条高亮显示
    override var preferredStatusBarStyle: UIStatusBarStyle{
        return UIStatusBarStyle.lightContent
    }
    
    //点开单元格后可以显示课件具体内容
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let detailController = CoursewareDetailController()
        detailController.coursewareModel = self.courseList[indexPath.row] as!CoursewareReturnJson
        //let model = self.courseList[indexPath.row] as! CoursewareReturnJson
        self.hidesBottomBarWhenPushed = true
        _ = navigationController?.pushViewController(detailController, animated: true)//详情界面进站啦
        self.hidesBottomBarWhenPushed = false

    }


}
